package com.eastmelon.blog.controller;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.basic.model.SystemContext;
import com.eastmelon.blog.entity.Article;
import com.eastmelon.blog.entity.Channel;
import com.eastmelon.blog.entity.Tags;
import com.eastmelon.blog.service.ArticleService;
import com.eastmelon.blog.service.ChannelService;
import com.eastmelon.blog.service.IndexService;
import com.eastmelon.blog.service.TagsService;
import com.eastmelon.blog.util.LuceneUtil;

@Controller
@RequestMapping("/back/article")
public class ArticleController {
	@Resource(name = "channelService")
	private ChannelService channelService;

	@Resource(name = "articleService")
	private ArticleService articleService;

	@Resource(name = "tagsService")
	private TagsService tagsService;
	
	@Resource(name = "indexService")
	private IndexService indexService;
	
	@RequestMapping("/articleInfor")
	public String showArticle(Model model) {
		SystemContext.setPageSize(5);
		Pager<Article> pa = articleService.findPage();
		model.addAttribute("pa", pa);
		return "back/article/articleInfor";
	}

	@RequestMapping(value = "/addArticle", method = RequestMethod.POST)
	public String addArticle(Article article, @RequestParam MultipartFile articleImg)
			throws Exception {
		if (!articleImg.isEmpty()) {
			//设置文章图片路径
			String exn = FilenameUtils.getExtension(articleImg.getOriginalFilename());
			String imageName = String.valueOf(new Date().getTime()) + "." + exn;
			article.setArticlePic(imageName);
		}
		String dateCategory = null;
		if(article.getPublishTime()!=null && !"".equals(article.getPublishTime())){
			//设置日期分类
			String[] d = article.getPublishTime().split("-");
			dateCategory = d[0] + "年" + d[1]+"月";
			article.setDateCategory(dateCategory);
		}
		if (article.getSummary().length() > 350) {
			String summary = article.getSummary().substring(0, 350);
			summary += "......";
			article.setSummary(summary);
		}
		if(article.getCid()==0)
		{
			article.setCid(null);
		}
		if(article.getTid()==0)
		{
			article.setTid(null);
		}
		articleService.save(article, articleImg.getInputStream());
		Article a =articleService.getLastItem();
		LuceneUtil.Indexer(a);
		indexService.createBody();
		indexService.createNav();
		return "redirect:/back/article/articleInfor.do";
	}

	@RequestMapping(value = "/addArticle", method = RequestMethod.GET)
	public String addArticle(Model model) {
		List<Channel> lc = channelService.find();
		List<Tags> tags=tagsService.find();
		model.addAttribute("lc", lc);
		model.addAttribute("tags", tags);
		return "back/article/articleAdd";
	}

	@RequestMapping("/delArticle")
	public String articleDel(int id) throws Exception {
		articleService.deleteById(id);
		indexService.createBody();
		indexService.createNav();
		LuceneUtil.delete(Integer.toString(id));
		return "redirect:/back/article/articleInfor.do";
	}

	@RequestMapping("/delArticles")
	public String delArticles(String ids) throws Exception {
		String[] idsS = ids.split(",");
		articleService.deleteByIds(idsS);
		for(String id:idsS){
			LuceneUtil.delete(id);
		}
		indexService.createBody();
		indexService.createNav();
		return "redirect:/back/article/articleInfor.do";
	}

	@RequestMapping(value = "/updateArticle", method = RequestMethod.GET)
	public String updateArticle(Model model, int id) {
		List<Channel> lc = channelService.find();
		List<Tags> tags=tagsService.find();
		Article a = articleService.get(id);
		model.addAttribute("tags", tags);
		model.addAttribute("lc", lc);
		model.addAttribute("a", a);
		return "back/article/articleEdit";
	}

	@RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
	public String editArticle(Article article, @RequestParam MultipartFile articleImg)
			throws Exception {
		if (!articleImg.isEmpty()){
			String exn = FilenameUtils.getExtension(articleImg.getOriginalFilename());
			String imageName = String.valueOf(new Date().getTime()) + "." + exn;
			article.setArticlePic(imageName);
		}
		String dateCategory = null;
		if(article.getPublishTime()!=null && !"".equals(article.getPublishTime())){
			//设置日期分类
			String[] d = article.getPublishTime().split("-");
			dateCategory = d[0] + "年" + d[1]+"月";
			article.setDateCategory(dateCategory);
		}
		if (article.getSummary().length() > 350) {
			String summary = article.getSummary().substring(0, 350);
			summary += "......";
			article.setSummary(summary);
		}
		if(article.getCid()==0)
		{
			article.setCid(null);
		}
		if(article.getTid()==0)
		{
			article.setTid(null);
		}
		String oldImageName = articleService.getOldImage(article.getId());
		articleService.update(article, articleImg.getInputStream(), oldImageName);
		LuceneUtil.update(article);
		indexService.createBody();
		indexService.createNav();
		return "redirect:/back/article/articleInfor.do";
	}
}
